package com.kee.server.job;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kee.common.util.JSoupUtil;
import com.kee.server.service.ExamServiceImpl;
import com.kee.server.service.IExamService;
import com.kee.share.bean.Exam;

public class TikuExamJob extends Thread {
	private final static String USER_AGTNT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 SE 2.X MetaSr 1.0";
	private final static IExamService service = new ExamServiceImpl();
	
	private final static String baseURL = "http://tiku.21cnjy.com/tiku.php?mod=quest&channel=2&cid=39&xd=2&type=1";
	
	private int pageNo = 3;
	
	private String subject = "语文";
	private String grade = "初中";
	
	
	@Override
	public void run() {
		for (int i = 1; i <= pageNo; i++) {
			Document doc = null;
			try {
				doc = Jsoup.connect(baseURL+"&page="+i).userAgent(USER_AGTNT).timeout(5000).get();
			} catch (IOException e1) {
				e1.printStackTrace();
				continue;
			}
			
			Element element  = doc.getElementsByClass("questions_col").first();
			Elements exams = element.select("li");
			int j = 0;
			for(Element exam : exams){
				String href = "http://tiku.21cnjy.com/" + exam.select("p a").first().attr("href");
				Exam e;
				try {
					e = JSoupUtil.tikudanxun(USER_AGTNT, href);
					if (e != null) {
						e.setGrade(grade);
						e.setSubject(subject);
						e.setType(1);
						e.setWebsite(2);
						e.setSection("现代文阅读");
						service.add(e);
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				j++;
				System.out.println("tiku===PageNo:" + i + "===Size:" + j);
			}
		}
	}
	
	public static void main(String[] args) {
		TikuExamJob tj = new TikuExamJob();
		tj.start();
	}
	
}
